package com.neusoft.neu24.mybatis02;

import com.neusoft.neu24.mybatis02.mapper.MemberMapper;
import com.neusoft.neu24.mybatis02.mapper.ProjectMapper;
import com.neusoft.neu24.mybatis02.mapper.RoleMapper;
import com.neusoft.neu24.nep.entity.Member;
import com.neusoft.neu24.nep.entity.Project;
import com.neusoft.neu24.nep.entity.Role;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MainApp {
    public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        ProjectMapper mapper = sqlSession.getMapper(ProjectMapper.class);
        List<Project> projects = mapper.selectAll();
        projects.forEach(System.out::println);


    }
    public static void main9(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        ProjectMapper mapper = sqlSession.getMapper(ProjectMapper.class);
       Project project =  mapper.selectById(1);
        System.out.println(project);


    }
    public static void main8(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        List<Role> lrs =  mapper.selectAll2();
        lrs.forEach(System.out::println);

    }
    public static void main7(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
        List<Role> lrs =  mapper.selectAll();
        lrs.forEach(System.out::println);

    }
    public static void main6(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        List<Member> members = mapper.m2oSelect2();
        members.forEach(System.out::println);
    }
    public static void main5(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        List<Member> members = mapper.m2oSelect();
        members.forEach(System.out::println);
    }

    public static void main4(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        List  list =  mapper.dselectById(new int[]{1,3,5,7});
        list.forEach(System.out::println);


    }


    public static void main3(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        Member member = new Member();
        member.setMname("实训中心1");
        member.setLogpwd("123456");
        member.setId(10);
        mapper.dmodMember(member);
        sqlSession.commit();

    }
    public static void main2(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        Member member = new Member();
        member.setMname("实训中心");
        member.setLogid("sxzx");
        mapper.daddMember(member);
        sqlSession.commit();

    }
    public static void main1(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

        SqlSession sqlSession = sqlSessionFactory.openSession();
//        获得Mapper接口的实现类
        MemberMapper mapper = sqlSession.getMapper(MemberMapper.class);
        Member member = new Member();
        member.setMname("自动ID");
        member.setBirthday( new Date() );
        member.setGender("女");
        List list =  mapper.selectByExample(member);
        list.forEach(  System.out::println );
    }
}
